package com.hualan.controller;

import com.hualan.domain.Article;
import com.hualan.service.ArticleServiceImpl;
import com.hualan.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.UUID;


@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleServiceImpl articleService;

    @GetMapping("/findThreeArticle")
    public R getThreeArticle(){
        return R.success(articleService.findThreeArticle());
    }

    @GetMapping("/findLikeArticle")
    public R getLikeArticle(String articleName){
        return R.success(articleService.findLikeArticle(articleName));
    }

    @PostMapping("/insertArticle")
    public R insertArtice(Article article){
        return R.success(articleService.insertArticle(article));
    }

    //获取所有的文章
    @GetMapping("/getAllArticle")
    public R getAllArticle(){
        return R.success(articleService.findAllArticle());
    }

    //修改文章内容
    @PostMapping("/updateArticle")
    public R updateArticle(Article article){
        return R.success(articleService.updateArticle(article));
    }

    //删除文章根据id
    @DeleteMapping("/deleteArticleById")
    public R deleteArticleById(Long id){
        return R.success(articleService.deleteArticle(id));
    }

    @GetMapping("/getArticleAndTeacher")
    public R getArticleAndTeacher(){
        return R.success(articleService.findArticleAndTeacher());
    }

    //文件上传
    @PostMapping("/upload")
    public R upload(MultipartFile file, HttpSession httpSession) throws IOException {
//        String path = "C:\\Users\\86175\\Desktop\\sfzy\\shi_fang_zhi_yu\\十方后端\\sfzy\\sfzyupload\\";
        String originalFilename = file.getOriginalFilename();
        String ext = originalFilename.substring(originalFilename.lastIndexOf("."));
        String filename = UUID.randomUUID().toString() + ext;
//        path = path + filename;
        //获取根路径
        ServletContext servletContext = httpSession.getServletContext();
        String rootPath = servletContext.getRealPath("uploadS");

        System.out.println(rootPath);
        System.out.println(File.separator);
        System.out.println(filename);

        //最终路径
        String finalPath = rootPath + File.separator + filename;
        System.out.println(finalPath);



        file.transferTo(new File(finalPath));

        return R.success(filename);
    }

    @GetMapping("/down")
    public void down(String filename, HttpServletResponse response, HttpSession httpSession) throws IOException {
        //获取根路径
        ServletContext servletContext = httpSession.getServletContext();
        String rootPath = servletContext.getRealPath("uploadS");

        //最终路径

        String finalPath = rootPath + File.separator + filename;
        System.out.println(finalPath);
        FileInputStream inputStream = new FileInputStream(finalPath);
        ServletOutputStream outputStream = response.getOutputStream();

        byte[] b = new byte[1024];
        int i = inputStream.read(b);
        while (i != 1){
            outputStream.write(b, 0, i);
            i = inputStream.read(b);
        }
        outputStream.close();
        inputStream.close();
    }

}
